package 回溯;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName _17电话号码的字母组合
 * @Description TODO
 * @Author 昝亚杰
 * @Date 2021/10/9 20:40
 * Version 1.0
 **/
public class _17电话号码的字母组合 {
    List<String> strings = new ArrayList<>();
    StringBuffer stringBuffer = new StringBuffer();
    public List<String> letterCombinations(String digits) {
        if(digits.length() == 0){
            return strings;
        }
        Map<Character,String> map = new HashMap();
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4',"ghi");
        map.put('5',"jkl");
        map.put('6',"mno");
        map.put('7',"pqrs");
        map.put('8',"tuv");
        map.put('9',"wxyz");
        getletterCombinations(0,digits,map);
        return strings;
    }
    public void getletterCombinations(int start,String digits, Map<Character,String> map){
        if(stringBuffer.length() == digits.length()){
            strings.add(new String(stringBuffer));
            return;
        }
        char c = digits.charAt(start);
        String o = map.get(c);
        for(char cc : o.toCharArray()){
            stringBuffer.append(cc);
            getletterCombinations(start+1,digits,map);
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        }
    }
}
